<include file='Index/header' />
<div class="row">
	<br>
	<form id="product_comment_form" action='{:U("Admin/Product/updateProductComment")}' method="post">
		<div class="col-sm-12">
			<br>
			<table id="product_comment_table" class="table table-hover table-bordered table-striped table-striped">
				<thead>
					<tr>
						<td>选择</td>
						<td>编号</td>
						<td>用户</td>
						<td>类型</td>
						<td>评论对象</td>
						<td>评论时间</td>
						<td>状态</td>
						<td>操作</td>
					</tr>
				</thead>
				<tbody>

				</tbody>
			</table>
		</div>
		<div class="col-sm-12">
			<div class="col-sm-2">
				<select name="status" class="form-control">
					<option value="0">禁止显示</option>
					<option value="1">允许显示</option>
					<option value="2">删除评论</option>
				</select>
			</div>
			<div class="col-sm-2">
				<a class="btn btn-primary" onclick="submit()">确&nbsp;定</a>
			</div>
		</div>
	</form>
</div>
<include file='Index/footer' />
<include file='Product/product-comment-detail' />
<script>
$(document).ready(function() {
	loadTable();		
});
var table;
function loadTable(){
	table = $('#product_comment_table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/Product/getProductComment/")}',
		"columns": [
					{ "data":'product_comment_id'},
					{ "data":'id'},
					{ "data":'user'},
					{ "data":'type'},
	        { "data": 'product' },
	        { "data": 'createtime' },
	        { "data": 'status' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
    	{
	    	"targets": 0,
	    	"render": function (data, type, row, table) {
	    		return '<input type="checkbox" name="product_comment_ids[]" value="'+data+'" />';
	      }
	    },
	    {
	    	"targets": 1,
	    	"render": function (data, type, row, table) {
	    		return table.row + 1;
	      }
	    },
	    {
	    	"targets": 2,
	    	"render": function (data, type, row, table) {
	    		return data == null? '' : data.name;
	      }
	    },
	    {
	    	"targets": 3,
	    	"render": function (data, type, row, table) {
	    		return "商品";
	      }
	    },
	    {
	    	"targets": 4,
	    	"render": function (data, type, row, table) {
	    		return data == null? '' : data.name;
	      }
	    },
	    {
	    	"targets": 6,
	    	"render": function (data, type, row, table) {
	    		return data == '0'? "不显示":"显示";
	      }
	    },
	    {
	    	"targets": -1,
	    	"data": null,
	    	"render": function (data, type, row, table) {
	    		var btn = '下架';
	    		var status = '1';
	    		if(row.status != '0'){
						btn = '上架';
						status = '0';
	    		}
	        return '<a class="btn btn-info" onclick="edit(\''+row.product_comment_id+'\')">查看详情</a> <a class="btn btn-warning" onclick="del(\''+row.product_comment_id+'\')">移除</a>';
	      },
	    }
    ]
	});
}

function edit(product_comment_id){
	$.ajax({
			url: '{:U("Admin/Product/getProductComment/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"product_comment_id":product_comment_id
			}
		})
		.success(function(data){
			$("#comment").html(data.user.name + " 于 " + data.createtime + " 对 " + data.product.name + " 发表评论<br>" + data.comment);

			if(data.status == '0'){
				$('#btn').attr('onclick', 'modStauts(\''+data.product_comment_id+'\',"1")');
				$('#btn').html('允许显示');
			}else{
				$('#btn').attr('onclick', 'modStauts(\''+data.product_comment_id+'\',"0")');
				$('#btn').html('禁止显示');
			}
			
			$('#product_comment_modal').modal({
				backdrop : "static"
			});
	});
}

function del(product_comment_id){
	if(!confirm("确认删除评论？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/Product/deleteProductComment/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"product_comment_id":product_comment_id
			}
		})
		.success(function(data){
			location.reload();
	});
}

function submit(){
	 $('#product_comment_form').ajaxSubmit({
        success:function(data){
            location.reload();
        }
    });
}
</script>